package com.waya.web.browser.util;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.webkit.WebView;
import java.lang.ref.WeakReference;

/**
 * Created by JesseHev on 2018/1/11.
 * describe: 调用js方法
 */

public class CallJsUtil {

  public static final String ACTION_INSTALL_APK = "ACTION_INSTALL_APK";

  public static final String ACTION_UNINSTALL_APK = "ACTION_UNINSTALL_APK";

  private static final String TAG = "CallJsUtil";

  //private WebView mWebView;

  private WeakReference<WebView> mWebWeakReference;

  private String mFuncName;

  private String mResult;

  private Activity mActivity;

  private boolean isReg = false;

  private Handler mHandler;

  public CallJsUtil() {
    mHandler = new Handler(Looper.getMainLooper());
  }

  public CallJsUtil setWebView(WebView webView) {
    mWebWeakReference = new WeakReference<>(webView);
    return this;
  }

  private BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override public void onReceive(Context context, Intent intent) {
      //switch (intent.getAction()) {
      //  case ACTION_INSTALL_APK:
      //    mWebView.loadUrl("javascript:" + mFuncName + "(\"" + mResult + "\")");
      //    break;
      //  case ACTION_UNINSTALL_APK:
      //    break;
      //  default:
      //    break;
      //}
      mResult = "{result:0}";
      mHandler.post(new Runnable() {
        @Override public void run() {
          mWebWeakReference.get().loadUrl("javascript:" + mFuncName + "(\"" + mResult + "\")");
        }
      });
    }
  };

  public CallJsUtil setReuslt(String funcName, String json) {
    this.mFuncName = funcName;
    this.mResult = json;
    return this;
  }

  public CallJsUtil register(Activity activity) {
    if (isReg) {
      Log.d(TAG, "broadcast have been registered!");
      return this;
    }
    this.mActivity = activity;
    isReg = true;
    IntentFilter filter = new IntentFilter();
    filter.addAction(ACTION_INSTALL_APK);
    filter.addAction(ACTION_UNINSTALL_APK);
    activity.registerReceiver(mReceiver, filter);
    return this;
  }

  public void sendBrodcast(String action) {
    if (!isReg) {
      Log.d(TAG, "please to register the broadcast...");
      return;
    }
    mActivity.sendBroadcast(new Intent(action));
  }

  public void unRegister() {
    if (!isReg) {
      Log.d(TAG, "please to register the broadcast... ");
      return;
    }
    if (mReceiver == null) {
      return;
    }
    mActivity.unregisterReceiver(mReceiver);
    isReg = false;
    mReceiver = null;
  }

  /**
   * 调用js方法
   *
   * @param funcName js方法名字
   * @param json 通知消息
   */
  public void callJs(final String funcName, final String json) {
    if (mWebWeakReference.get() == null) {
      Log.d(TAG, "callJs: webview is null");
      return;
    }
    mHandler.post(new Runnable() {
      @Override public void run() {
        mWebWeakReference.get().loadUrl("javascript:" + funcName + "(\"" + json + "\")");
      }
    });
  }
}
